package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectInfoConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.application.service.SubjectInfoApplicationService;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;

import com.jingdinajichi.subject.common.entity.PageResult;
import com.jingdinajichi.subject.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zwy
 * @version 1.0
 * @description: SubjectController
 * @date 2025/7/4 20:30
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectInfoApplicationService subjectInfoApplicationService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectInfoController.add.subjectInfoDTO:{}", JSON.toJSON(subjectInfoDTO));
        }

            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectParse(), "题目解析不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectCategoryIds(), "分类id列表不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectLabelIds(), "标签id列表不能为空");
//            Preconditions.checkNotNull(subjectInfoDTO.getSubjectOptionList(), "选项列表不能为空");
            
            SubjectInfoBO subjectInfoBO = SubjectInfoConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            subjectInfoApplicationService.add(subjectInfoBO);

            //TODO 调用领域服务

            return Result.ok(true) ;

    }
    /**
     * 查询题目列表
     * @param subjectInfoDTO（继承pageInfo）
     *  将subjectInfoDTO转化为subjectInfoBO
     *                      抛给领域服务方法getSubjectPage
     *                      返回PageResult<SubjectInfoBO>
     *                      通过MapStruct转化为 PageResult<SubjectInfoDTO>
     * @return PageResult<SubjectInfoDTO>
     */
    @GetMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubject(
                @RequestParam(required = false) Integer pageNum,
                @RequestParam(required = false) Integer pageSize,
                @ModelAttribute SubjectInfoDTO subjectInfoDTO
    ){

            if(log.isInfoEnabled()){
                log.info("SubjectInfoController.getSubject.subjectInfoDTO:{}", JSON.toJSON(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> subjectInfoBOPageResult = subjectInfoApplicationService.getSubjectPage(pageNum, pageSize, subjectInfoBO);
//            log.info("subjectInfoBOPageResult:{}", JSON.toJSON(subjectInfoBOPageResult));
            PageResult<SubjectInfoDTO> subjectInfoDTOPageResult = SubjectInfoConverter.INSTANCE.convertBOPageResultToDTO(subjectInfoBOPageResult);
//            log.info("subjectInfoDTOPageResult:{}", JSON.toJSON(subjectInfoDTOPageResult));
//            TODO 调用领域服务

            return Result.ok(subjectInfoDTOPageResult) ;


    }
    /**
     * 查询题目详情
     * @param subjectInfoDTO
     * @return SubjectInfoDTO
     */
    @GetMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@ModelAttribute SubjectInfoDTO subjectInfoDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectInfoController.querySubjectInfo.subjectInfoDTO:{}", JSON.toJSON(subjectInfoDTO));
        }

            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
              subjectInfoBO = subjectInfoApplicationService.querySubjectInfo(subjectInfoBO);
            return Result.ok(SubjectInfoConverter.INSTANCE.convertBOToDTO(subjectInfoBO));

    }


}
